package com.alibaba.cloud.ddd.infrastructure.messaging.jms;

import java.lang.invoke.MethodHandles;

import org.openqa.selenium.remote.http.TextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.jms.Message;
import jakarta.jms.MessageListener;
import com.alibaba.cloud.ddd.application.CargoInspectionService;
import com.alibaba.cloud.ddd.domain.model.cargo.TrackingId;

/**
 * Consumes JMS messages and delegates notification of misdirected cargo to the
 * tracking service.
 *
 * This is a programmatic hook into the JMS infrastructure to make cargo
 * inspection message-driven.
 */
public class CargoHandledConsumer implements MessageListener {

	private final CargoInspectionService cargoInspectionService;
	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	public CargoHandledConsumer(CargoInspectionService cargoInspectionService) {
		this.cargoInspectionService = cargoInspectionService;
	}

	@Override
	public void onMessage(final Message message) {
		try {
			final TextMessage textMessage = (TextMessage) message;
			final String trackingidString = textMessage.text();

			cargoInspectionService.inspectCargo(new TrackingId(trackingidString));
		} catch (Exception e) {
			logger.error("Error consuming CargoHandled message", e);
		}
	}
}
